﻿Imports Microsoft.AspNet.Identity
Imports Microsoft.AspNet.Identity.EntityFramework
Imports System.Collections.Generic
Imports System.ComponentModel.DataAnnotations
Imports System.ComponentModel.DataAnnotations.Schema
Imports System.Data.Entity
Imports System.Data.Entity.Infrastructure
Imports System.Data.Entity.Validation
Imports System.Linq
Imports System.Web

Public Class User
    Implements IUser
    Public Sub New()
        Me.New("")
    End Sub

    Public Sub New(userName__1 As String)
        UserName = userName__1
        Id = Guid.NewGuid().ToString()
    End Sub

    <Key> _
    Public Property Id() As String Implements IUser.Id
        Get
            Return m_Id
        End Get
        Set(value As String)
            m_Id = value
        End Set
    End Property
    Private m_Id As String

    Public Property UserName() As String
        Get
            Return m_UserName
        End Get
        Set(value As String)
            m_UserName = Value
        End Set
    End Property
    Private m_UserName As String
End Class
Public Class UserLogin
    Implements IUserLogin
    <Key, Column(Order:=0)> _
    Public Property LoginProvider() As String Implements IUserLogin.LoginProvider
        Get
            Return m_LoginProvider
        End Get
        Set(value As String)
            m_LoginProvider = value
        End Set
    End Property
    Private m_LoginProvider As String
    <Key, Column(Order:=1)> _
    Public Property ProviderKey() As String Implements IUserLogin.ProviderKey
        Get
            Return m_ProviderKey
        End Get
        Set(value As String)
            m_ProviderKey = value
        End Set
    End Property
    Private m_ProviderKey As String

    Public Property UserId() As String Implements IUserLogin.UserId
        Get
            Return m_UserId
        End Get
        Set(value As String)
            m_UserId = value
        End Set
    End Property
    Private m_UserId As String

    Public Sub New()
    End Sub

    Public Sub New(userId__1 As String, providerName As String, providerAccountKey As String)
        LoginProvider = providerName
        ProviderKey = providerAccountKey
        UserId = userId__1
    End Sub
End Class

Public Class UserSecret
    Implements IUserSecret
    Public Sub New()
    End Sub

    Public Sub New(userName__1 As String, secret__2 As String)
        UserName = userName__1
        Secret = secret__2
    End Sub

    <Key> _
    Public Property UserName() As String Implements IUserSecret.UserName
        Get
            Return m_UserName
        End Get
        Set(value As String)
            m_UserName = value
        End Set
    End Property
    Private m_UserName As String
    Public Property Secret() As String Implements IUserSecret.Secret
        Get
            Return m_Secret
        End Get
        Set(value As String)
            m_Secret = value
        End Set
    End Property
    Private m_Secret As String
End Class

Public Class Role
    Implements IRole
    Public Sub New()
        Me.New("")
    End Sub

    Public Sub New(roleName As String)
        Id = roleName
    End Sub

    <Key> _
    Public Property Id() As String Implements IRole.Id
        Get
            Return m_Id
        End Get
        Set(value As String)
            m_Id = value
        End Set
    End Property
    Private m_Id As String
End Class

Public Class UserRole
    Implements IUserRole
    <Key, Column(Order:=0)> _
    Public Property RoleId() As String Implements IUserRole.RoleId
        Get
            Return m_RoleId
        End Get
        Set(value As String)
            m_RoleId = value
        End Set
    End Property
    Private m_RoleId As String
    <Key, Column(Order:=1)> _
    Public Property UserId() As String Implements IUserRole.UserId
        Get
            Return m_UserId
        End Get
        Set(value As String)
            m_UserId = value
        End Set
    End Property
    Private m_UserId As String
End Class

Public Class IdentityDbContext
    Inherits DbContext
    Public Sub New()
        MyBase.New("DefaultConnection")
    End Sub

    Protected Overrides Function ValidateEntity(entityEntry As DbEntityEntry, items As IDictionary(Of Object, Object)) As DbEntityValidationResult
        If entityEntry.State = EntityState.Added Then
            Dim user As User = TryCast(entityEntry.Entity, User)
            'check for uniqueness of user name
            If user IsNot Nothing AndAlso Users.Where(Function(u) u.UserName.ToUpper() = user.UserName.ToUpper()).Count() > 0 Then
                Dim result = New DbEntityValidationResult(entityEntry, New List(Of DbValidationError)())
                result.ValidationErrors.Add(New DbValidationError("User", "UserName must be unique."))
                Return result
            End If
        End If
        Return MyBase.ValidateEntity(entityEntry, items)
    End Function

    Public Property Users() As DbSet(Of User)
        Get
            Return m_Users
        End Get
        Set(value As DbSet(Of User))
            m_Users = value
        End Set
    End Property
    Private m_Users As DbSet(Of User)
    Public Property Logins() As DbSet(Of UserLogin)
        Get
            Return m_Logins
        End Get
        Set(value As DbSet(Of UserLogin))
            m_Logins = value
        End Set
    End Property
    Private m_Logins As DbSet(Of UserLogin)
    Public Property Secrets() As DbSet(Of UserSecret)
        Get
            Return m_Secrets
        End Get
        Set(value As DbSet(Of UserSecret))
            m_Secrets = value
        End Set
    End Property
    Private m_Secrets As DbSet(Of UserSecret)
    Public Property Roles() As DbSet(Of Role)
        Get
            Return m_Roles
        End Get
        Set(value As DbSet(Of Role))
            m_Roles = value
        End Set
    End Property
    Private m_Roles As DbSet(Of Role)
    Public Property UserRoles() As DbSet(Of UserRole)
        Get
            Return m_UserRoles
        End Get
        Set(value As DbSet(Of UserRole))
            m_UserRoles = value
        End Set
    End Property
    Private m_UserRoles As DbSet(Of UserRole)
End Class